package com.manong.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.manong.domain.Goods;
import com.manong.domain.Role;
import com.manong.exception.BusinessExp;
import com.manong.mapper.GoodsMapper;
import com.manong.query.GoodsQueryObject;
import com.manong.service.GoodsService;
import com.manong.vo.RolePermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public List<Goods> getGoods() {
        return goodsMapper.selectAll();
    }

    @Override
    public PageInfo query(GoodsQueryObject queryObject) {
        if(queryObject.getPageSize() == -1){
            List<Goods> goods = goodsMapper.selectForList(queryObject);
            return new PageInfo(goods);
        } else {
            //PageHelper进行分页操作
            //告诉PageHelper 当前页是多少 每页显示数据
            PageHelper.startPage(queryObject.getPageNum(), queryObject.getPageSize());
            //调用分页方法
            List<Goods> goods = goodsMapper.selectForList(queryObject);
            return new PageInfo(goods);
        }
    }

    @Override
    public void delete(Long id) {
        if (id == null) {
            throw new BusinessExp("参数不能为空");
        }
        try {
            int count = goodsMapper.deleteByPrimaryKey(id);
            // count 影响行数， 如果count == 0 表示删除失败
            if (count == 0) {
                throw new BusinessExp("删除失败");
            }

            // 删除关系表信息
//        goodsMapper.deleteRelation(id);
        } catch (Exception e) {
            if (e.getMessage().contains("Cannot delete or update a parent row: a foreign key constraint fails")) {
                throw new BusinessExp("该商品在售中，不能删除");
            } else {
                throw new BusinessExp("删除过程中发生错误: " + e.getMessage());
            }
        }
    }

    @Override
    public void saveOrUpdate(Goods goods) {
        if(goods == null){
            throw new BusinessExp("非法操作");
        }
        if(goods.getGoods_id() == null){
            this.save(goods);
        } else {
            this.update(goods);
        }
    }

    @Transactional
    @Override
    public void save(Goods goods) {
        // 拿到role对象
        Goods goodsInfo = goods;
        // 把role存在数据库库中
        goodsMapper.insert(goodsInfo);
    }

    @Override
    public void update(Goods goods) {
        // vo对象中获取role对象
        Goods goodsInfo = goods;
        goodsMapper.updateByPrimaryKey(goodsInfo);
    }

    @Override
    public Goods get(Long id) {
        if(id == null){
            throw new BusinessExp("id不能为空");
        }
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        if(goods == null){
            throw new BusinessExp("非法操作");
        }
        return goods;
    }

    @Override
    public int getTotalGoodsCount() {
        return goodsMapper.selectAll().size();
    }

}
